﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarchEstate.Manager.BusinesEntity;


public class MakeFormController : AbstractJqueryController
{
    /// <summary>
    /// 批量添加表单
    /// </summary>
    public void BatchAddMakeForm()
    {
        try
        {
            int empId = 1;//Convert.ToInt32(sessionData["empId"]);
            int meterType = Convert.ToInt32(ParamsData["meterType"]);
            int meterType2 = Convert.ToInt32(ParamsData["meterType2"]);
            AbstractMeterHead head = NewObject<AbstractMeterHead>();
            head.BatchAddMakeForm(meterType, meterType2, empId, empId);
            TxtJson = ReturnSuccess("表单生成成功！");
        }
        catch (Exception err)
        {
            TxtJson = ReturnError(err.Message);
        }
    }
    /// <summary>
    /// 创建空表单
    /// </summary>
    public void AddEmptyMakeForm()
    {
        int empId = 1;//Convert.ToInt32(sessionData["empId"]);
        int meterType = Convert.ToInt32(ParamsData["meterType"]);
        int meterType2 = Convert.ToInt32(ParamsData["meterType2"]);
        AbstractMeterHead head = NewObject<AbstractMeterHead>();

        head.MeterHeadId = null;
        head.BillNo = DateTime.Now.ToString("yyyyMMddHHmmss") + "00";
        head.MeterType = meterType;
        head.MeterType2 = meterType2;
        head.MeterDate = DateTime.Now;
        head.CompleteFlag = 0;
        head.GetEmpId = empId;
        head.GetEmpName = CoreFrame.Business.CommonData.GetData("EmpName", empId).ToString();
        head.EmpId = empId;
        head.EmpName = CoreFrame.Business.CommonData.GetData("EmpName", empId).ToString();
        head.Save();

        TxtJson = ReturnSuccess("空表单创建成功！");
    }
    /// <summary>
    /// 获取表单头
    /// </summary>
    public void GetMakeformList()
    {
        int meterType = Convert.ToInt32(ParamsData["meterType"]);
        int meterType2 = Convert.ToInt32(ParamsData["meterType2"]);
        AbstractMeterHead head = NewObject<AbstractMeterHead>();
        List<AbstractMeterHead> headList = head.GetMeterHeadList(meterType, meterType2, 0);
        TxtJson = base.ListToJson(headList);
    }
    /// <summary>
    /// 删除指定表单
    /// </summary>
    public void DeleteMakeform()
    {
        try
        {
            string makeheadId = ParamsData["makeheadId"];
            AbstractMeterHead head = NewObject<AbstractMeterHead>();
            head.Delete(makeheadId);
            TxtJson = ReturnSuccess("删除成功！");
        }
        catch (Exception err)
        {
            TxtJson = ReturnError("删除失败!" + err.Message);
        }
    }
    /// <summary>
    /// 得到表单明细
    /// </summary>
    public void GetMakeOrderList()
    {
        string MeterHeadId = ParamsData["MeterHeadId"];
        int currpagenum = Convert.ToInt32(ParamsData["page"]);
        int pagesize = Convert.ToInt32(ParamsData["rows"]);
        CoreFrame.Core.Orm.PageInfo page = new CoreFrame.Core.Orm.PageInfo(pagesize, currpagenum);

        AbstractMeterOrder order = NewObject<AbstractMeterOrder>();
        List<AbstractMeterOrder> orderList = order.GetMeterOrderList(MeterHeadId, page);
        TxtJson = base.ListToJson(orderList, page.totalRecord);
    }

    /// <summary>
    /// 保存抄表值
    /// </summary>
    public void SaveThisNum()
    {
        string meterOrderId = ParamsData["meterOrderId"];
        decimal thisNum = Convert.ToDecimal(ParamsData["thisNum"]);
        AbstractMeterOrder order = NewObject<AbstractMeterOrder>();
        order = order.UpdateMeterformResult(meterOrderId, thisNum);
        TxtJson = ReturnSuccess("", base.ModelToJson(order));
    }

    /// <summary>
    /// 删除一条表单明细
    /// </summary>
    public void DeleteMakeOrder()
    {
        string MeterOrderId = ParamsData["MeterOrderId"];
        AbstractMeterOrder order = NewObject<AbstractMeterOrder>();
        order.Delete(MeterOrderId);
        TxtJson = ReturnSuccess("删除成功！");
    }
    /// <summary>
    /// 得到基础用表情况
    /// </summary>
    public void GetMakerList()
    {
        int meterType = Convert.ToInt32(ParamsData["meterType"]);
        int meterType2 = Convert.ToInt32(ParamsData["meterType2"]);
        int currpagenum = Convert.ToInt32(ParamsData["page"]);
        int pagesize = Convert.ToInt32(ParamsData["rows"]);
        CoreFrame.Core.Orm.PageInfo page = new CoreFrame.Core.Orm.PageInfo(pagesize, currpagenum);
        AbstractMeter meter = NewObject<AbstractMeter>();
        List<AbstractMeter> meterlist = meter.GetMeterList(meterType, meterType2, page);
        TxtJson = base.ListToJson(meterlist, page.totalRecord);
    }
    /// <summary>
    /// 查询基础用表情况
    /// </summary>
    public void SearchMakerList()
    {
        string searchKey = ParamsData["searchKey"];
        int meterType = Convert.ToInt32(ParamsData["meterType"]);
        int meterType2 = Convert.ToInt32(ParamsData["meterType2"]);
        int currpagenum = Convert.ToInt32(ParamsData["page"]);
        int pagesize = Convert.ToInt32(ParamsData["rows"]);
        CoreFrame.Core.Orm.PageInfo page = new CoreFrame.Core.Orm.PageInfo(pagesize, currpagenum);
        AbstractMeter meter = NewObject<AbstractMeter>();
        List<AbstractMeter> meterlist = meter.GetMeterList(meterType, meterType2, searchKey, page);
        TxtJson = base.ListToJson(meterlist, page.totalRecord);
    }
    /// <summary>
    /// 添加表单明细
    /// </summary>
    public void AddMakerOrder()
    {
        int empId = 1;//Convert.ToInt32(sessionData["empId"]);
        string MeterHeadId = ParamsData["MeterHeadId"];
        string[] MeterIds = ParamsData["MeterIds"].Split(new char[]{','});

        AbstractMeter meter = NewObject<AbstractMeter>();        
        AbstractMeterOrder _order = NewObject<AbstractMeterOrder>();
        for (int i = 0; i < MeterIds.Length; i++)
        {
            meter.InitDao();
            meter = meter.GetMeter(MeterIds[i]);
            _order.MeterOrderId = null;
            _order.MeterHeadId = MeterHeadId;
            _order.MeterId = meter.MeterId;
            _order.OwnerId = meter.OwnerId;
            _order.OwnerName = meter.OwnerName;
            _order.SrccId = meter.SrccId;
            _order.SrccName = meter.SrccName;
            _order.LastMeterDate = meter.LastMeterDate;
            _order.ThisMeterDate = DateTime.Now;
            _order.LastNum = meter.LastNum;
            _order.ThisNum = 0;
            _order.MeterNum = 0;
            _order.Thiscost = 0;
            _order.Save();
        }
        TxtJson = ReturnSuccess("添加成功！");
    }
    /// <summary>
    /// 审核表单
    /// </summary>
    public void AuditingMeterfrom()
    {
        try
        {
            string MeterHeadId = ParamsData["MeterHeadId"];
            AbstractMeterHead head = NewObject<AbstractMeterHead>();
            head.AuditingMeterform(MeterHeadId);
            TxtJson = ReturnSuccess("审核成功，完成抄表！");
        }
        catch (Exception err)
        {
            TxtJson = ReturnError("审核失败，" + err.Message);
        }
    }

    public void GetHistoryMakeformList()
    {
        DateTime bdate = Convert.ToDateTime(Convert.ToDateTime(ParamsData["bdate"]).ToString("yyyy-MM-dd")+" 00:00:00");
        DateTime edate = Convert.ToDateTime(Convert.ToDateTime(ParamsData["edate"]).ToString("yyyy-MM-dd")+" 23:59:59");
        int meterType = Convert.ToInt32(ParamsData["meterType"]);
        int meterType2 = Convert.ToInt32(ParamsData["meterType2"]);

        int currpagenum = Convert.ToInt32(ParamsData["page"]);
        int pagesize = Convert.ToInt32(ParamsData["rows"]);
        CoreFrame.Core.Orm.PageInfo page = new CoreFrame.Core.Orm.PageInfo(pagesize, currpagenum);


        AbstractMeterHead head = NewObject<AbstractMeterHead>();
        List<AbstractMeterHead> headList = head.GetMeterHeadList(meterType, meterType2, 1,bdate,edate,page);
        TxtJson = base.ListToJson(headList,page.totalRecord);
    }
}

